/**
 * Title :	jFamilyTree generator
 * Description: jFamilyTree generator
 * Version:	provider
 * Copyright (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.generator;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;

import jFamilyTree.Messages;

/**
 * Tool bar for tree view
 * 
 * @author Bernard Bou
 */
public class TreeToolbar extends JToolBar
{
	private static final long serialVersionUID = 1L;

	// D A T A

	/**
	 * Controller (command sink)
	 */
	private Controller theController;

	/**
	 * Vertical layout
	 */
	static public final boolean isVertical = true;

	// C O N S T R U C T O R

	/**
	 * Constructor
	 */
	public TreeToolbar()
	{
		super(isVertical ? VERTICAL : HORIZONTAL);
		setFloatable(true);
		setPreferredSize(new Dimension(36, 20 + 4 * 36));
		initialize();
	}

	/**
	 * Initialize
	 */
	private void initialize()
	{
		JButton thisNewElementButton = makeButton(Messages.getString("TreeToolbar.10"), Messages.getString("TreeToolbar.11"), "new.png"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		thisNewElementButton.addActionListener(new java.awt.event.ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				theController.execute(Controller.Code.NEWELEMENT, 0);
			}
		});

		JButton thisDeleteElementButton = makeButton(Messages.getString("TreeToolbar.20"), Messages.getString("TreeToolbar.21"), "delete.png"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		thisDeleteElementButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				theController.execute(Controller.Code.REMOVEELEMENT, 0);
			}
		});
		this.add(thisNewElementButton);
		this.add(thisDeleteElementButton);

		JButton thisExpandButton = makeButton(Messages.getString("TreeToolbar.30"), Messages.getString("TreeToolbar.31"), "expand.png"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		thisExpandButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				theController.execute(Controller.Code.EXPANDTREE, 0);
			}
		});
		JButton thisCollapseButton = makeButton(Messages.getString("TreeToolbar.40"), Messages.getString("TreeToolbar.41"), "collapse.png"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		thisCollapseButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				theController.execute(Controller.Code.COLLAPSETREE, 0);
			}
		});
		this.add(thisNewElementButton);
		this.add(thisDeleteElementButton);
		this.add(thisExpandButton);
		this.add(thisCollapseButton);

	}

	/**
	 * Make theButton
	 * 
	 * @param thisText
	 *        text
	 * @param thisTooltip
	 *        tooltip
	 * @param thisImage
	 *        image
	 * @return theButton
	 */
	private JButton makeButton(String thisText, String thisTooltip, String thisImage)
	{
		JButton thisButton = new JButton();
		thisButton.setToolTipText(thisTooltip);
		thisButton.setIcon(new ImageIcon(TreeToolbar.class.getResource("images/" + thisImage))); //$NON-NLS-1$
		return thisButton;
	}

	/**
	 * Set controller
	 * 
	 * @param thisController
	 *        controller
	 */
	public void setController(Controller thisController)
	{
		theController = thisController;
	}
}
